  <template>
    <div>
      <div class="el_submit">
        <el-button type="primary" @click="openAddDiaog()" style="margin-bottom:10px">新增</el-button>
      </div>
      <el-table border :data="tableData">
        <el-table-column label="序号" align="center" width="60">
          <template slot-scope="scope">
            {{scope.$index + 1}}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100">
          <template slot-scope="scope">
            <el-button type="text" @click="delRow(scope.row.categoryId)">删除</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="categoryId" label="品类ID" v-if="false"></el-table-column>
        <el-table-column prop="categoryName" label="品类名称" width="280" align="center"></el-table-column>
        <el-table-column prop="name" label="创建人" align="center" width="180"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center" width="180"></el-table-column>
      </el-table>
      <el-dialog title="新增" :visible.sync="dialogFormVisible">
        <el-form :model="vdCategoryForm" ref="vdCategoryForm" style="width: 80%;" label-width="150px">
          <el-form-item label="品类:">
            <select-tree :multiple="false" ref="selectTree" v-model="vdCategoryForm.categoryName"></select-tree>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="resetForm('vdCategoryForm'); dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="add(vdCategoryForm)">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
<script>
   import SelectTree from '@/components/common/select-ext/select-tree.vue';
   export default {
    components: {
      SelectTree
    },
    data() {
      return {
        dialogFormVisible:false,
        tableData:[],
        vdCategoryForm:{
          categoryId:'',
          categoryName:'',
          mainFlag:1,
          vendorId:this.$route.query.id
        }
      }
    },
    mounted(){ // VUE页面渲染之后调用的方法
      this.getDlvApply();
    },
     // 监听品类下拉
    watch: {
      'vdCategoryForm.categoryName':function(val){
         debugger;
         let data = this.$refs['selectTree'].getResultData();
         if(!!data.id){
           this.vdCategoryForm.categoryId = data.id;
         }
      }
    },
    methods: {
     // 查询供应商开发申请
      getDlvApply() {
        this.axios.post(this.lib.config.host.vendorMgt + "/vendormgt/vendorcategory/getByVendorId/" + this.$route.query.id)
          .then((res) => {
            this.tableData = res.data;
          })
          .catch((error) => {
            console.log(error);
          });
      },
      // 打开新增对话框
      openAddDiaog(){
        this.dialogFormVisible = true;
      },
       // 删除行
      delRow(categoryId) {
        alert(categoryId);
        this.axios.post(this.lib.config.host.vendorMgt + "/vendormgt/vendorcategory/delete/" + this.$route.query.id + "/" + categoryId)
          .then((res) => {
            this.getDlvApply();
          })
          .catch((error) => {
            console.log(error);
          });
      },
      // 新增
      add(vdCategoryForm) {
        this.axios.post(this.lib.config.host.vendorMgt + '/vendormgt/vendorcategory/add', vdCategoryForm)
          .then((response) => {
            if (response.data.success){
              this.$message({
                type: 'success',
                message: '新增成功！'
              });
              this.dialogFormVisible = false;
              this.getDlvApply();
            } else {
              this.$message({
                type: 'warning',
                message: response.data.message
              });
            }
          })
          .catch(function (error) {
            console.log(error);
          });
      },
    }
  }
</script>